package cn.jingyuan.swan.gw.config;

import cn.jingyuan.swan.gw.locator.RemoteRouteLocator;
import cn.jingyuan.swan.gw.locator.ResourceLocator;
import cn.jingyuan.swan.gw.service.AuthorityService;
import cn.jingyuan.swan.gw.service.RouteService;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class AppConfig {

    @Resource
    RouteService routeService;

    @Resource
    RemoteRouteLocator routeLocator;

    @Resource
    AuthorityService authorityService;

    @Bean
    public RemoteRouteLocator routeLocator(
        ServerProperties serverProperties,
        ZuulProperties zuulProperties,
        ApplicationEventPublisher eventPublisher) {
        String contextPath = serverProperties.getServlet().getContextPath();
        return new RemoteRouteLocator(contextPath, zuulProperties, routeService, eventPublisher);
    }

    @Bean
    public ResourceLocator resourceLocator() {
        return new ResourceLocator(routeLocator, authorityService);
    }

}
